exports.install = function(Vue) {
	Vue.prototype.goLink = (url) => {
		if (url === '/pages/index/index' || url === '/pages/index/my' || url === '/pages/index/rank') {
			uni.switchTab({
				url: url
			})
		} else {
			uni.navigateTo({
				url: url
			})
		}
	}
	Vue.prototype.goBack = () => {
		let pages = getCurrentPages();
		if (pages.length === 1 || pages.length === 0) {
			return uni.reLaunch({
				url: `/pages/index/index`
			});
		}
		let prevPage = pages[pages.length - 2]; //上一页页面实例
		// #ifdef MP
		prevPage.onLoad(prevPage.__displayReporter.query)
		uni.navigateBack()
		// #endif
		// #ifdef H5 
		uni.reLaunch({
			url: `/${prevPage.route}`
		});
		// #endif
	}
	Vue.prototype.goLogin = () => {
		uni.showModal({
			title: '提示',
			content: '您还未登录,是否前往登录?',
			confirmText: '登录',
			success: function(res) {
				if (res.confirm) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
				}
			}
		});
	}
	Vue.prototype.goUpload = (arr) => {
		return new Promise(resolve => {
			uni.chooseImage({
				count: 1,
				success: function(res) {
					uni.uploadFile({
						url: 'http://43.138.191.23/api/upload',
						filePath: res.tempFilePaths[0],
						name: 'file',
						header: {
							'authorization-tx': `Bearere ${uni.getStorageSync('USERINFO').token}`
						},
						success: (uploadFileRes) => {
							resolve()
							const OBJ = JSON.parse(uploadFileRes.data)
							if (OBJ.code === 200) {
								return arr.push(OBJ.data.file_url);
							} else {
								uni.showToast({
									title: OBJ.msg,
									icon: "none"
								})
							}
						},
					});
				},
			});
		})
	}
	Vue.prototype.goPreview = (index, arr) => {
		uni.previewImage({
			urls: arr,
			current: index
		})
	}


	Vue.prototype.goSaveAlbum = (data, is_url) => {
		function saveImageToPhotosAlbum(data, is_url) {
			uni.showLoading({
				title: '保存图片中'
			})
			if (is_url) {
				data.forEach((item) => {
					if (item.trim().length === 0) {
						uni.hideLoading()
						return uni.showToast({
							title: '没有图片资源',
							icon: "error"
						})
					}
					// #ifdef H5
					let a = document.createElement('a');
					a.href = item;
					a.download = item;
					a.click();
					uni.hideLoading()
					uni.showToast({
						title: '保存成功',
					})
					// #endif
					uni.downloadFile({
						header: {
							'content-disposition': 'attachment'
						},
						url: `${item}?${Date.now()}`,
						success: (res) => {
							// #ifdef MP
							if (res.statusCode === 200) {
								uni.saveImageToPhotosAlbum({
									filePath: res.tempFilePath,
									success: () => {
										uni.hideLoading()
										uni.showToast({
											title: '保存成功',
										})
									},
									fail() {
										uni.hideLoading()
										uni.showToast({
											title: '保存失败',
											icon: "error"
										})
									}
								})
							}
							// #endif
						},
						fail() {
							uni.hideLoading()
							uni.showToast({
								title: '保存失败',
								icon: "error"
							})
						}
					})
				})
			} else {
				base64 = data.replace(/^data:image\/\w+;base64,/, ""); //去掉data:image/png;base64,
				let filePath = wx.env.USER_DATA_PATH + '/goods_poster' + Math.floor(Math.random() *
						1000000) +
					'.png'
				uni.getFileSystemManager().writeFile({
					filePath: filePath, //创建一个临时文件名
					data: base64, //写入的文本或二进制数据
					encoding: 'base64', //写入当前文件的字符编码
					success: () => {
						uni.hideLoading()
						uni.saveImageToPhotosAlbum({
							filePath: filePath,
							success: () => {
								uni.showToast({
									title: '保存成功',
								})
							},
							fail: () => {
								uni.showToast({
									title: '保存失败',
									icon: "error"
								})
							}
						})
					},
					fail: () => {
						uni.hideLoading()
						uni.showToast({
							title: '保存失败',
							icon: 'error'
						})
					}
				})
			}
		}
		// #ifdef H5
		saveImageToPhotosAlbum(data, is_url)
		// #endif
		// #ifdef MP
		uni.getSetting({ //获取用户的当前设置
			success: (res) => {
				if (res.authSetting['scope.writePhotosAlbum']) { //验证用户是否授权可以访问相册
					saveImageToPhotosAlbum(data, is_url);
				} else {
					uni.authorize({ //如果没有授权，向用户发起请求
						scope: 'scope.writePhotosAlbum',
						success: () => {
							saveImageToPhotosAlbum(data, is_url);
						},
						fail: () => {
							uni.showToast({
								title: "请打开保存相册权限，再点击保存相册",
								icon: "none",
								duration: 2000
							});
							setTimeout(() => {
								uni.openSetting({ //调起客户端小程序设置界面,让用户开启访问相册
									success: () => {}
								});
							}, 2000);
						}
					})
				}
			}
		})
		// #endif
	}
	Vue.prototype.goCopy = (text) => {
		if ((text || '').trim().length == 0) {
			return uni.showToast({
				title: '复制失败',
				icon: "error"
			})
		}
		uni.setClipboardData({
			data: text,
			success: () => {
				uni.showToast({
					title: '复制成功'
				})
			},
			fail: () => {
				uni.showToast({
					title: '复制失败',
					icon: "error"
				})
			}
		})

	}
}
